﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmChuyenHocVienSangTuVanKhac : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        private DataSet ds = new DataSet();
        private string _companyID = GlobalVariable.WSInfo.CompanyID;

        public frmChuyenHocVienSangTuVanKhac()
        {
            InitializeComponent();
        }

        private void frmChuyenHocVienSangTuVanKhac_Load(object sender, EventArgs e)
        {
            GetData();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            UpdateData();
            GetData();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
            GetData();
        }

        #region "Coding"
        private void LoadEnquiry()
        {
            try
            {
                if (ds.Tables["TableEnquiry"] != null)
                    ds.Tables["TableEnquiry"].Clear();
                string sql = @"SELECT
                            EnquiryNumber, Holot, Ten, Counsellor
                            FROM Enquiry
                            WHERE MaDonVi = '" + _companyID + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableEnquiry");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadUser()
        {
            try
            {
                if (ds.Tables["TableUser"] != null)
                    ds.Tables["TableUser"].Clear();
                string sql = @"SELECT UserID, FullName FROM SYS_List_User";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void GetData()
        {
            try
            {
                LoadEnquiry();
                LoadUser();
                gridControl_HocVien.DataSource = ds;
                gridControl_HocVien.DataMember = "TableEnquiry";
                repositoryItemSearchLookUpEdit_Counsellor.DataSource = ds.Tables["TableUser"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"SELECT
                            EnquiryNumber, Holot, Ten, Counsellor
                            FROM Enquiry
                            WHERE MaDonVi = '" + _companyID + "'";
                SqlDataAdapter da_enq = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da_enq);
                da_enq.Update(ds, "TableEnquiry");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
